const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const {
  merge
} = require("webpack-merge");
const webpackBaseConfig = require("./webpack.base.config.js");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const {
  CleanWebpackPlugin
} = require("clean-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin;
const AddAssetHtmlPlugin = require("add-asset-html-webpack-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const PurgecssPlugin = require("purgecss-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const BuildInfo = require("./version.js");
// const glob = require('glob')

// const PATHS = {
//     src: path.join(__dirname, 'src')
// }

module.exports = merge(webpackBaseConfig, {
  mode: "production",
  module: {
    rules: [
      // 处理css
      {
        test: /\.css$/i,
        use: [{
            loader: MiniCssExtractPlugin.loader,
            options: {
              esModule: true,
            },
          },
          "css-loader",
          {
            //postcss-loader插件，用于自动补全样式的浏览器前缀
            loader: "postcss-loader",
            options: {
              plugins: [require("autoprefixer")({})],
            },
          },
        ],
      },
      // 处理less
      {
        test: /\.less$/i,
        use: [{
            loader: MiniCssExtractPlugin.loader,
            options: {
              esModule: true,
            },
          },
          "css-loader",
          "less-loader",
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      favicon: "./icon.ico",
      inject: true,
      chunks: ["main"],
    }),
    new HtmlWebpackPlugin({
      filename: "./version/index.html",
      template: "./version.html",
      inject: false, //不插入生成的js 仅用于版本声明
      minify: {
        removeComments: false,
        collapseWhitespace: true,
        removeAttributeQuotes: true,
      },
      buildInfo: BuildInfo,
    }),
    new VueLoaderPlugin(),
    new CleanWebpackPlugin(),
    // new BundleAnalyzerPlugin(),
    new webpack.DllReferencePlugin({
      manifest: path.resolve(__dirname, "dll/manifest.json"),
    }),
    new AddAssetHtmlPlugin({
      filepath: path.resolve(__dirname, "dll/iview.dll.js"),
    }),
    new MiniCssExtractPlugin({
      filename: "[name].[contenthash:8].css",
    }),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.css$/g,
      cssProcessor: require("cssnano"),
      cssProcessorPluginOptions: {
        preset: ["default", {
          discardComments: {
            removeAll: true
          }
        }],
      },
      canPrint: true,
    }),

    // new PurgecssPlugin({
    //     paths: glob.sync(`${PATHS.src}/**/*`,  { nodir: true }),
    // }),
  ],
  optimization: {
    minimizer: [
      new TerserPlugin({
        parallel: true,
        cache: true,
        terserOptions: {
          compress: {
            // drop_console: true,
          },
          output: {
            comments: false,
          },
        },
      }),
    ],
  },
});